<template>
  <div class="login">
    <div class="login-box">
      <!-- 头像 -->
      <div class="login-img">
        <img src="@/assets/head.jpg" alt="" />
      </div>
      <!-- 表单区 -->
      <div class="login-form">
        <el-form ref="form" :rules="rules" :model="form" label-width="60px">
          <el-form-item label="账号" prop="username">
            <el-input
              prefix-icon="el-icon-user-solid"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              v-model="form.password"
              type="password"
            ></el-input>
          </el-form-item>
          <el-button type="primary" @click="handellogin">登录</el-button>
          <el-button type="info" @click="handelremove">重置</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/index";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handellogin() {
      //登录前表单预校验
      this.$refs.form.validate((valid) => {
        //预校验失败返回提示信息
        if (!valid) return this.$message.error("账号密码不符合要求");
        //校验通过
        login(this.form).then((res) => {
          console.log(res.data);
          if (res.data.meta.status === 200) {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            //将token保存到会话缓存中
            window.sessionStorage.setItem("token", res.data.data.token);
            //跳转页面
            this.$router.push("/home");
          } else {
            this.$message({
              message: res.data.meta.msg,
              type: "warning",
            });
          }
        });
      });
    },
    handelremove() {
      this.form = {};
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url("@/assets/1280-720.jpg");

  .login-box {
    height: 300px;
    width: 450px;
    background-color: white;
    border-radius: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .login-img {
      height: 100px;
      width: 100px;
      border-radius: 50px;
      overflow: hidden;
      background-color: black;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        width: 100%;
      }
    }

    .login-form {
      height: 100%;
      width: 100%;
      text-align: center;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
